package com.example.app2024.counttime;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.app2024.R;


public class MainActivity extends Activity implements OnClickListener
{

      private EditText inputet;
      private Button getTime, startTime, stopTime;
      private TextView time;
      private int i = 0;
      private Timer timer = null;
      private TimerTask task = null;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.counttime_main);
            initView();
      }

      private void initView()
      {
            inputet = (EditText) findViewById(R.id.inputtime);
            getTime = (Button) findViewById(R.id.gettime);
            startTime = (Button) findViewById(R.id.starttime);
            stopTime = (Button) findViewById(R.id.stoptime);
            time = (TextView) findViewById(R.id.time);
            getTime.setOnClickListener(this);
            startTime.setOnClickListener(this);
            stopTime.setOnClickListener(this);
      }

      @Override
      public void onClick(View v)
      {

            if (v.getId() == R.id.gettime)
            {
                  time.setText(inputet.getText().toString());
                  i = Integer.parseInt(inputet.getText().toString());
            }
            else if (v.getId() == R.id.starttime)
            {
                  startTime();
            }
            else if (v.getId() == R.id.stoptime)
            {
                  stopTime();
            }
      }

      final private Handler mHandler = new MyHandler();

      public void startTime()
      {
            timer = new Timer();
            task = new TimerTask()
            {

                  @Override
                  public void run()
                  {
                        i--;
                        Message message = mHandler.obtainMessage();
                        message.arg1 = i;

                        mHandler.sendMessage(message);
                  }
            };
            timer.schedule(task, 1);
      }

      public void stopTime()
      {
            timer.cancel();
      }

      private class MyHandler extends Handler
      {
            @Override
            public void handleMessage(Message msg)
            {
                  time.setText(msg.arg1 + "");
                  startTime();
            }
      }
}
